﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using Microsoft.Office.Interop.Word;
using System.Text.RegularExpressions;

namespace SanctusSoftware.Music.ChordSheetEditor.Word
{
  
  public class WdColorStringConverter : IValueConverter
  {
    
   



    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      if (!(value is WdColor))
      {
        return string.Empty;
      }
      else
      {
        string temp = value.ToString();
        
        // Remove Prefix
        if (temp.StartsWith("wdColor"))
        {
          temp = temp.Substring("wdColor".Length);
        }

        // Split Up Words
        string pattern = @"((?<=[a-z0-9])[A-Z])|((?<=[a-zA-Z])[0-9])";
        MatchCollection matches = Regex.Matches(temp, pattern, RegexOptions.None);
        for (int i = matches.Count - 1; i >= 0; i--)
        {
          var match = matches[i];
          temp = temp.Insert(match.Index, " ");
        }

        // Format 2-digit Percent Numbers
        pattern = @"(?<![0-9])[0-9]{2}(?![0-9])";
        matches = Regex.Matches(temp, pattern, RegexOptions.None);
        for (int i = matches.Count - 1; i >= 0; i--)
        {
          var match = matches[i];
          temp = temp.Insert(match.Index + match.Length, "%");
        }

        // Format 3-digit Percent Numbers
        pattern = @"(?<![0-9])[0-9]{3}(?![0-9])";
        matches = Regex.Matches(temp, pattern, RegexOptions.None);
        for (int i = matches.Count - 1; i >= 0; i--)
        {
          var match = matches[i];
          temp = temp.Insert(match.Index + match.Length, "%");
          temp = temp.Insert(match.Index + match.Length - 1, ".");
        }

        return temp;
      }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      throw new NotImplementedException();
    }

  }

}
